package com.b1.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.FileUtils;
import com.b1.constant.CommonConstant;
import com.b1.mapper.TeacherSleepRecordMapper;
import com.b1.pojo.dto.partolSleepRecord.PartolSleepExportRecord;
import com.b1.pojo.dto.teacherSleepRecord.TeacherSleepExportRecord;
import com.b1.pojo.enity.PartolSleepRecord;
import com.b1.pojo.dto.teacherSleepRecord.TeacherSleepRecordQueryRequest;
import com.b1.pojo.enity.ChildernSleepRecord;
import com.b1.pojo.enity.TeacherSleepRecord;
import com.b1.pojo.enums.YesOrNoEnum;
import com.b1.service.TeacherSleepRecordService;
import com.b1.utils.AliOssUtil;
import com.b1.utils.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import java.util.Date;

/**
* @author ThinkBook
* @description 针对表【teacher_sleep_record(教师的关于幼儿午睡情况管理表格)】的数据库操作Service实现
* @createDate 2024-10-09 15:46:27
*/
@Service
public class TeacherSleepRecordServiceImpl extends ServiceImpl<TeacherSleepRecordMapper, TeacherSleepRecord>
    implements TeacherSleepRecordService{
    @Autowired
    private AliOssUtil aliOssUtil;


    @Override
    public QueryWrapper<TeacherSleepRecord> getQueryWrapper(TeacherSleepRecordQueryRequest teacherSleepRecordQueryRequest) {
        QueryWrapper<TeacherSleepRecord> queryWrapper = new QueryWrapper<>();

        String className = teacherSleepRecordQueryRequest.getClassName();
        String kindomName = teacherSleepRecordQueryRequest.getKindomName();
        Integer bedNum = teacherSleepRecordQueryRequest.getBedNum();
        Integer actualNum = teacherSleepRecordQueryRequest.getActualNum();
        String bedWetName = teacherSleepRecordQueryRequest.getBedWetName();
        Date bedWetTime = teacherSleepRecordQueryRequest.getBedWetTime();
        String feverName = teacherSleepRecordQueryRequest.getFeverName();
        Date feverTime = teacherSleepRecordQueryRequest.getFeverTime();
        String otherCaseName = teacherSleepRecordQueryRequest.getOtherCaseName();
        String otherCase = teacherSleepRecordQueryRequest.getOtherCase();
        String caseHandle = teacherSleepRecordQueryRequest.getCaseHandle();
        String teacher = teacherSleepRecordQueryRequest.getTeacher();
        String admin = teacherSleepRecordQueryRequest.getAdmin();
        Date checkTime = teacherSleepRecordQueryRequest.getCheckTime();
        String sortField = teacherSleepRecordQueryRequest.getSortField();
        String sortOrder = teacherSleepRecordQueryRequest.getSortOrder();


        //模糊查询
        queryWrapper.like(StringUtils.isNotBlank(otherCase), "otherCase", otherCase);
        queryWrapper.like(StringUtils.isNotBlank(caseHandle), "caseHandle", caseHandle);

        //精确查询
        queryWrapper.eq(StringUtils.isNotEmpty(className), "className", className);
        queryWrapper.eq(StringUtils.isNotEmpty(kindomName), "kindomName", kindomName);
        queryWrapper.eq(ObjectUtils.isNotEmpty(bedNum), "bedNum", bedNum);
        queryWrapper.eq(ObjectUtils.isNotEmpty(actualNum), "actualNum", actualNum);
        queryWrapper.eq(StringUtils.isNotEmpty(bedWetName), "bedWetName", bedWetName);
        queryWrapper.eq(StringUtils.isNotEmpty(feverName), "feverName", feverName);
        queryWrapper.eq(StringUtils.isNotEmpty(otherCaseName), "otherCaseName", otherCaseName);
        queryWrapper.eq(StringUtils.isNotEmpty(teacher), "teacher", teacher);
        queryWrapper.eq(StringUtils.isNotEmpty(admin), "admin", admin);
        //时间范围查询
        queryWrapper.between(ObjectUtils.isNotEmpty(checkTime), "checkTime", checkTime, checkTime);

        //排序
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;

    }
    @Override
    public String export(Long[] ids) throws IOException {
        List<TeacherSleepRecord> teacherSleepRecord = listByIds(Arrays.asList(ids));
        List<TeacherSleepExportRecord> teacherSleepExportRecord = new ArrayList<>();
        for (TeacherSleepRecord record : teacherSleepRecord) {
            TeacherSleepExportRecord export = new TeacherSleepExportRecord();
            BeanUtil.copyProperties(record,export);
            teacherSleepExportRecord.add(export);
        }
        String upload;
        //创建一个临时文件写入
        File file = new File("teacherSleepExportRecord.xlsx");
        try (FileOutputStream fos = new FileOutputStream(file)) {
            //将数据写入临时文件
            EasyExcel.write(fos, PartolSleepExportRecord.class).sheet("教师的关于幼儿午睡情况管理表格").doWrite(teacherSleepExportRecord);
        }catch (Exception exception){
            throw new RuntimeException("导出失败");
        }
        //将临时文件上传到阿里云oss
        String fileName="教师的关于幼儿午睡情况管理表格"+System.currentTimeMillis()+".xlsx";
        //将 file转为byte
        byte[] bytes = FileUtils.readFileToByteArray(file);
        upload = aliOssUtil.upload(bytes, fileName);
        return upload;
    }
}




